package com.netintech.project.system.controller;

import com.netintech.common.constant.UserConstants;
import com.netintech.common.utils.StringUtils;
import com.netintech.core.aspectj.lang.annotation.Log;
import com.netintech.core.aspectj.lang.enums.BusinessType;
import com.netintech.core.web.controller.BaseController;
import com.netintech.core.web.domain.RequestResult;
import com.netintech.project.system.domain.LevelDict;
import com.netintech.project.system.service.LevelDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import static com.netintech.core.web.domain.RequestResult.error;

/**
 * 级联字典表控制类
 * 
 * @author admin
 */
@RestController
@RequestMapping("/system/levelDict")
public class SysLevelDictController extends BaseController
{

    @Autowired
    private LevelDictService dictService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public RequestResult list(LevelDict dict)
    {
        return RequestResult.success(dictService.selectLevelDictList(dict));
    }

    /**
     * 新增字典取父级菜单
     */
    @GetMapping("/add/{parentId}")
    public RequestResult add(@PathVariable("parentId") Long parentId)
    {
        LevelDict dict = dictService.selectDictById(parentId);
        if(dict == null){
            dict = new LevelDict();
            dict.setDictId(0L);
            dict.setParentLabel("无");
        }
        return RequestResult.success(dict);
    }

    /**
     * 新增保存字典
     */
    @Log(title = "字典管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @PostMapping("/add")
    public RequestResult addSave(@RequestBody @Validated LevelDict dict)
    {
        if (UserConstants.LEVEL_DICT_NAME_NOT_UNIQUE.equals(dictService.checkDictLabelUnique(dict)))
        {
            return error("新增字典'" + dict.getDictLabel() + "'失败，字典名称已存在");
        }
        return returnResult(dictService.insertDict(dict));
    }

    /**
     * 修改获取字典数据
     */
    @GetMapping("/edit/{dictId}")
    public RequestResult edit(@PathVariable("dictId") Long dictId)
    {
        LevelDict dict = dictService.selectDictById(dictId);
        if (StringUtils.isNotNull(dict) && 0L == dictId)
        {
            dict.setParentLabel("无");
        }
        return RequestResult.success(dict);
    }

    /**
     * 修改保存
     */
    @Log(title = "字典管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PostMapping("/edit")
    public RequestResult editSave(@RequestBody@Validated LevelDict dict)
    {
        if (UserConstants.LEVEL_DICT_NAME_NOT_UNIQUE.equals(dictService.checkDictLabelUnique(dict)))
        {
            return error("修改字典'" + dict.getDictLabel() + "'失败，字典名称已存在");
        }
        else if(dict.getParentId().equals(dict.getDictId()))
        {
            return error("修改字典'" + dict.getDictLabel() + "'失败，上级字典不能是自己");
        }
        return returnResult(dictService.updateDict(dict));
    }

    /**
     * 删除
     */
    @Log(title = "字典管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/{dictId}")
    public RequestResult remove(@PathVariable("dictId") Long dictId)
    {
        if (dictService.selectDictCount(dictId) > 0)
        {
            return RequestResult.error("存在下级字典,不允许删除");
        }
        return RequestResult.success(dictService.deleteDictById(dictId));
    }

    /**
     * 校验字典名称是否唯一
     */
    @PostMapping("/checkDictLabelUnique")
    public RequestResult checkDictLabelUnique(@RequestBody LevelDict dict)
    {
        return RequestResult.success(dictService.checkDictLabelUnique(dict));
    }

    /**
     * 选择字典树
     */
    @GetMapping("/selectDictTree/{dictId}")
    public RequestResult selectDictTree(@PathVariable("dictId") Long dictId)
    {
        return RequestResult.success(dictService.selectDictById(dictId));
    }

    /**
     * 加载字典列表树
     */
    @GetMapping("/treeData")
    public RequestResult treeData()
    {
        return RequestResult.success(dictService.selectDictTree(new LevelDict()));
    }

}
